import { getEnv } from '@tarojs/taro';
import { PureComponent } from 'react';

export default class ErrorBoundaryCustom extends PureComponent<any> {
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
  constructor(props: any) {
    super(props);
  }
  // componentDidMount() {}
  UNSAFE_componentWillMount() {
    if (getEnv() === 'WEB') {
      window.addEventListener(
        'error',
        (event: any) => {
          if (event.message) {
            if (process.env.NODE_ENV === 'development') {
              console.log(event.message);
            }
          }
        },
        true,
      );
      window.addEventListener('unhandledrejection', (event: any) => {
        if (event.message) {
          if (process.env.NODE_ENV === 'development') {
            console.log(event.message);
          }
        }
      });
    }
  }
  componentDidCatch(error: any, errorInfo: any) {
    if (process.env.NODE_ENV === 'development') {
      console.error('componentDidCatch', error, errorInfo);
    }

    // const showError = {
    //   code: 'BoundaryError',
    //   message: '渲染出现了小故障',
    //   data: { error, errorInfo },
    // };
    // this.props.setError(showError);
  }

  render() {
    return this.props.children;
  }
}
